package com.cyys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cyys.entity.CategoryGoods;
import com.cyys.entity.ShoppingCart;
import com.cyys.response.Result;
import com.cyys.service.CategoryGoodsService;
import com.cyys.service.MiniUserService;
import com.cyys.service.ShoppingCartService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/shopping_cart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //商品
    @Autowired
    private CategoryGoodsService categoryGoodsService;

    /**
     *
     * @param user_id 用户id
     * @param dish_id  商品id
     * @param number 数量
     * @param dish_flavor
     * @return
     */
    @GetMapping("/addCart")
    public Result addShoppingCart(
            @RequestParam(name = "user_id",value = "user_id") Integer user_id,
            @RequestParam(name = "dish_id",value = "dish_id") Integer dish_id,
            @RequestParam(name = "number",value = "number") Integer number,
            @RequestParam(name = "dish_flavor",value = "dish_flavor",required = false) String dish_flavor
    ){

        //根据商品id查寻商品详情
        CategoryGoods cg = categoryGoodsService.getById(dish_id);

        //查询用户购物车是否有该商品
        QueryWrapper<ShoppingCart> qw = new QueryWrapper();
        qw.and(i->i.eq("user_id",user_id).eq("dish_id",dish_id).eq("dish_flavor",dish_flavor));
         ShoppingCart sc= shoppingCartService.getOne(qw);

                //判断有没有相同数据
            if (sc!=null){
                if (sc.getDish_flavor().equals(dish_flavor)){
                    shoppingCartService.updateNumberById(sc.getNumber()+number,sc.getId());
                }else {
                    //如果有相同的商品修改数量
                    getCart(user_id, dish_id, number, dish_flavor, cg);
                }
            }else {
                //没有则直接添加购物车
                getCart(user_id, dish_id, number, dish_flavor, cg);

            }

        return  Result.ok().message("加入购物车成功");
    }

    @GetMapping("/shoppingCartList")
    private void getCart(@RequestParam(name = "user_id", value = "user_id") Integer user_id, @RequestParam(name = "dish_id", value = "dish_id") Integer dish_id, @RequestParam(name = "number", value = "number") Integer number, @RequestParam(name = "dish_flavor", value = "dish_flavor", required = false) String dish_flavor, CategoryGoods cg) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setName(cg.getName());
        shoppingCart.setImage(cg.getMainimg());
        shoppingCart.setUser_id(user_id);
        shoppingCart.setDish_id(dish_id);
        shoppingCart.setNumber(number);
        shoppingCart.setDish_flavor(dish_flavor);
        shoppingCart.setAmount(cg.getPrice());
        shoppingCartService.save(shoppingCart);
    }

    @GetMapping("/cartList")
    public Result findCartListByUserId(
             Integer user_id
    ){
        QueryWrapper<ShoppingCart> ew= new QueryWrapper<>();
        ew.eq("user_id",user_id);
        List<ShoppingCart> list = shoppingCartService.list(ew);
        return Result.ok().data("list",list);
    }

    @GetMapping("/updateCart")
    public Result updateCartById(
            @RequestParam(value = "id",  required = false) Integer id,
            @RequestParam(value = "number",  required = false) Integer number
    ){
        QueryWrapper<ShoppingCart> qw = new QueryWrapper();
        qw.eq("id",id);
        ShoppingCart sc= shoppingCartService.getOne(qw);

        shoppingCartService.updateNumberById(sc.getNumber()+(number),id);
        ShoppingCart sc2= shoppingCartService.getOne(qw);
        if (sc2.getNumber()<=0){

            QueryWrapper<ShoppingCart> qw2 = new QueryWrapper();
            qw2.eq("number",0);
            shoppingCartService.remove(qw2);
        }
        return Result.ok().message("更改购物车成功");
    }
    @GetMapping("/deleteCart")
    public Result deleteCart(  @RequestParam(value = "id",  required = false) Long id
                             ){

        boolean b = shoppingCartService.removeById(id);
        if (!b){
         return    Result.error().message("删除购物车失败");
        }else {
            return Result.ok().message("删除购物车成功");
        }
    }

    @DeleteMapping("/delete/{uid}")
    public Result deleteClass( @RequestBody List<Integer> idLists,
                               @PathVariable Integer uid
    ){
        boolean b1 []=new boolean[idLists.size()];
        idLists.forEach(System.out::println);
        for (int i = 0; i <idLists.size() ; i++) {
            boolean b2 = shoppingCartService.removeById(idLists.get(i));
            b1[i]=b2;
            if (b1.equals("fales")) {
                return Result.error().message("删除购物车失败");
            }
        }
        return Result.ok().message("删除购物车成功");
    }

    @DeleteMapping("deleteCart/{user_id}")
    public Result deleteCart(@PathVariable Integer user_id){
        QueryWrapper qw  = new QueryWrapper();
        qw.eq("user_id",user_id);

        boolean remove = shoppingCartService.remove(qw);
        if (remove) {
            return Result.ok().message("删除购物车成功");
        }else {
            return Result.ok().message("删除购物车error");
        }
    }


}

